package cn.ptu.pmaa.controller;

import cn.ptu.pmaa.entities.*;
import cn.ptu.pmaa.service.StudentService;
import cn.ptu.pmaa.service.TeacherService;
import cn.ptu.pmaa.utils.TokenUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/teacher")
public class TeacherController {

    @Autowired
    TokenUtil tokenUtil;
    @Autowired
    TeacherService teacherService;
    @Autowired
    StudentService studentService;

    //通过预约时间查询教室
    @RequestMapping("/getsitebytime")
    public RespBean getSiteByTime(@RequestParam String date, @RequestParam String fromtime , @RequestParam String totime,@RequestParam int pageNum,@RequestParam int pageSize){
        //System.out.println("#3333");
        PageHelper.startPage(pageNum,pageSize);
        List<ClassRoom> classroom = teacherService.getSiteByTime(fromtime, totime, date);
        PageInfo p = new PageInfo<ClassRoom>(classroom);
        HashMap<String,Object> data = new HashMap<>();
        data.put("pages",p.getPages());
        data.put("total",p.getTotal());
        data.put("classroom",classroom);
        return new RespBean("200","成功",data);
    }

    //确定预约教室
    @RequestMapping("/yySiteByClassId")
    public RespBean yySiteByClassId(@RequestParam String jsid, @RequestParam String yyrq, @RequestParam String fromtime , @RequestParam String totime, HttpServletRequest request){
        String token = request.getHeader("token");
        Map<String,String> map = tokenUtil.parseToken(token);
        String userId = map.get("userId");
        String role = map.get("role");
        Appintment appintment = new Appintment(0,jsid,userId,yyrq,fromtime,totime,role,"1");

        if (teacherService.yySiteByClassId(appintment)==1){
            return new RespBean("200","插入成功");
        }else {
            return new RespBean("400","插入失败");
        }
    }

    //查看预约情况
    @RequestMapping("/ckyyqk")
    public RespBean ckyyqk(HttpServletRequest httpServletRequest,@RequestParam int pageNum,@RequestParam int pageSize){
        String token = httpServletRequest.getHeader("token");
        Map<String,String> map = tokenUtil.parseToken(token);
        String userId = map.get("userId");
        PageHelper.startPage(pageNum,pageSize);
        List<Appintment> ckyyqk = teacherService.ckyyqk(userId);
        List<HashMap<String,String>> datalist = new ArrayList<>();
        for (Appintment app:ckyyqk
        ) {
            HashMap<String,String> inner = new HashMap<>();
            ClassRoom c = studentService.getClassroomById(app.getYydjsid());
            inner.put("yyid",app.getYyid()+"");
            inner.put("jsld",c.getJsld());
            inner.put("jsfh",c.getJsfh());
            inner.put("yyrq",app.getYyrq());
            inner.put("yykssj",app.getYykssj());
            inner.put("yyjssj",app.getYyjssj());
            datalist.add(inner);
        }
        HashMap<String,Object> data = new HashMap<>();
        PageInfo p = new PageInfo<Appintment>(ckyyqk);
        data.put("pages",p.getPages());
        data.put("total",p.getTotal());
        data.put("appintment",datalist);
        return new RespBean("200","查询成功",data);
    }

    //取消预约
    @RequestMapping("/qxyy")
    public RespBean qxyy(HttpServletRequest httpServletRequest,@RequestParam("yyid") String yyid){
        String token = httpServletRequest.getHeader("token");
        Map<String,String> map = tokenUtil.parseToken(token);
        String userId = map.get("userId");
        int flag = teacherService.qxyy(userId,yyid);
        if(flag==1){
            return new RespBean("200","操作成功");
        }
        return new RespBean("400","操作失败");
    }

    //提前结束预约
    @RequestMapping("/tqjssy")
    public RespBean tqjssy(HttpServletRequest httpServletRequest,@RequestParam String yyid){
        String token = httpServletRequest.getHeader("token");
        Map<String,String> map = tokenUtil.parseToken(token);
        String userId = map.get("userId");
        int flag = teacherService.tqjssy(userId,yyid);
        if(flag==1){
            return new RespBean("200","操作成功");
        }
        return new RespBean("400","操作失败");
    }

    //个人信息修改
    @RequestMapping("/grxxxg")
    public RespBean grxxxg(Teacher teacher, HttpServletRequest httpServletRequest){
        String token = httpServletRequest.getHeader("token");
        Map<String,String> map = tokenUtil.parseToken(token);
        String userId = map.get("userId");
        teacher.setTeacherId(userId);//System.out.println(teacher);
        if(teacherService.grxxxg(teacher)==1){
            return new RespBean("200","操作成功");
        }else {
            return new RespBean("400","操作失败");
        }
    }

}
